﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.Common;
using Code.Models;
using System.Text.RegularExpressions;

namespace Code.BLL.Arithmetic
{
    /// <summary>
    /// 彩种抽像工厂
    /// </summary>
    public abstract class LotteryFactory
    {
        /// <summary>
        /// 根据彩种类型获取相应的彩种实例
        /// </summary>
        /// <param name="lottery"></param>
        /// <returns></returns>
        public static LotteryTypeProduct CreateLottery(LotteryType lottery)
        {
            switch (lottery)
            {
                case LotteryType.CQSSC:
                    return new CQSSC();
                case LotteryType.FC3D:
                    return new FC3D();
                case LotteryType.HLJSSC:
                    return new HLJSSC();
                case LotteryType.JXSSC:
                    return new JXSSC();
                case LotteryType.SHSSL:
                    return new SHSSL();
                case LotteryType.XJSSC:
                    return new XJSSC();
                case LotteryType.SZPL:
                    return new PL3();
                case LotteryType.TC11X5:
                    return new TC11X5();
                case LotteryType.HaoCai1:
                    return new HC1();
                case LotteryType.BJKL8:
                    return new BJKL8();
               case LotteryType.GDKLSF:
                   return new GDKLSF();
                default:
                    throw new ArgumentOutOfRangeException("没有当前彩种");
            }
        }
    }
    /// <summary>
    /// 玩法抽像工厂
    /// </summary>
    public abstract class LotteryTypeProduct
    {
        /// <summary>
        /// 根据玩法类型获取相应的玩法
        /// </summary>
        /// <param name="playType"></param>
        /// <returns></returns>
        public abstract PlayTypeProduct GetPlayType(PlayType_E playType);
    }
    public abstract class PlayTypeProduct
    {
        /// <summary>
        /// 当前实例化此玩法的彩种
        /// </summary>
        public LotteryType lotteryType { get; set; }
        private Regex _UserChoseDoubleNumberFormat;
        protected List<string> ChoseNumberList =new List<string>();

        public PlayTypeProduct(Regex regDouble)
        {
            this._UserChoseDoubleNumberFormat = regDouble;
        }
        /// <summary>
        /// 获取中奖注的号码列表
        /// </summary>
        /// <param name="userChoseNumber">用户选择的号码 格式：表示多星时用‘，’隔开，如：三星复式，234，567，23。表示单星的复式时无需隔开，如：三星复式，235679</param>
        /// <param name="openCode">当前彩种的开奖号码</param>
        /// <returns></returns>
        public IEnumerable<string> GetWinNums(string userChoseNumber, string openCode)
        {

            
            var code = openCode.Split(',');
            
            string open = "";
            for (int i = 0; i < code.Length; i++)
            {
                open += Convert.ToInt32(code[i]) + ",";
            }
            open = open.Substring(0, open.LastIndexOf(','));
            List<string> result = new List<string>();
            var list = userChoseNumber.Split(new string[]{"; ",";"}, StringSplitOptions.RemoveEmptyEntries);
            if (code.Length == 8)
            {
                foreach (var item in list)
                    result.AddRange(_GetWinNums(item, open));
            }
            else
            {
                foreach (var item in list)
                    result.AddRange(_GetWinNums(item, openCode));
            }
           
            return result;
        }
        /// <summary>
        /// 验证用户购买的号码的格式是否正确 
        /// </summary> 
        /// <param name="userChoseNumber">需要验证的字符串</param>
        /// <returns>如果匹配成功则返回原字符串，否则返回null</returns>
        public string RegistNum(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { "; ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in list)
                if (string.IsNullOrEmpty(_RegistNum(item)))
                    return null;
            return userChoseNumber;
        }
        /// <summary>
        /// 获取号码的注数(号码未经验证)
        /// </summary>
        /// <param name="userChoseNumber"></param>
        /// <returns></returns>
        public int GetNumCount(string userChoseNumber)
        {
            int i=0;
            var list = userChoseNumber.Split(new string[] { "; ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in list)
                i += _GetNumCount(item);
            return i;
        }
        public int RenXuanEr(string userChoseNumber) 
        {
          
            int result = 0;
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            int count = 0;
            int number = 0;
            foreach (var item in list)
            {
                number++;
            }
            if (number == 1)
            {
                count = str1.Length;
            }
            else
            {
                var num2 = list[1].ToString();
                string[] str2 = num2.Split(' ');
                count = str1.Length + str2.Length;
            }
           
            var n = 1;
            for (var a = count - 1; a <= count; a++)
            {
                n = n * a;
            }
            var m = 1;
            for (var i = 1; i < 3; i++)
            {
                m = m * i;
            }
            result = n / m;

            return result;
        }
        public int RenXuanSan(string userChoseNumber)
        {
            int result = 0;
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            int count = 0;
            int number = 0;
            foreach (var item in list)
            {
                number++;
            }
            if (number == 1)
            {
                count = str1.Length;
            }
            else
            {
                var num2 = list[1].ToString();
                string[] str2 = num2.Split(' ');
                count = str1.Length + str2.Length;
            }
            var n = 1;
            for (var a = count - 2; a <= count; a++)
            {
                n = n * a;
            }
            var m = 1;
            for (var i = 1; i < 4; i++)
            {
                m = m * i;
            }
            result = n / m;

            return result;
        }
        public int RenXuanSi(string userChoseNumber)
        {
            int result = 0;
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            int count = 0;
            int number = 0;
            foreach (var item in list)
            {
                number++;
            }
            if (number == 1)
            {
                count = str1.Length;
            }
            else
            {
                var num2 = list[1].ToString();
                string[] str2 = num2.Split(' ');
                count = str1.Length + str2.Length;
            }
            var n = 1;
            for (var a = count - 3; a <= count; a++)
            {
                n = n * a;
            }
            var m = 1;
            for (var i = 1; i < 5; i++)
            {
                m = m * i;
            }
            result = n / m;

            return result;
        }
        public int RenXuanWu(string userChoseNumber)
        {
            int result = 0;
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            int count = 0;
            int number = 0;
            foreach (var item in list)
            {
                number++;
            }
            if (number == 1)
            {
                count = str1.Length;
            }
            else
            {
                var num2 = list[1].ToString();
                string[] str2 = num2.Split(' ');
                count = str1.Length + str2.Length;
            }
            var n = 1;
            for (var a = count - 4; a <= count; a++)
            {
                n = n * a;
            }
            var m = 1;
            for (var i = 1; i < 6; i++)
            {
                m = m * i;
            }
            result = n / m;

            return result;
        }
        public int RenErDanTuo(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            int count =str2.Length;
            if (str1.Count() > 1 || str2.Count() + str1.Count() < 3 || str1.Count() == 0)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            return count;
        }
        public int RenSanDanTuo(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            var result = 0;
            int count = str1.Length;
            int counynum = str2.Length;
            if (str1.Count() > 2 || str2.Count() + str1.Count() < 4 || str1.Count() == 0)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            if (count == 1 && counynum > 1)
            {
                var n = 1;
                for (var a = counynum - 1; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 3; i++)
                {
                    m = m * i;
                }
                result = n / m;

            }
            if (count == 2 && counynum > 0)
            {
                result = counynum;
            }
            if (count == 0 || (counynum + count) < 3)
            {
                result = 0;
            }
            return result;
        }
        public int RenSiDanTuo(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            int result = 0;
            int count = str1.Length;
            int counynum = str2.Length;
            if (str1.Count() > 3 || str2.Count() + str1.Count() < 5 || str1.Count() == 0)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            if (count == 1 && counynum > 2)
            {
                var n = 1;
                for (var a = counynum - 2; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 4; i++)
                {
                    m = m * i;
                }
                result = n / m;

            }
            if (count == 2 && counynum > 1)
            {
                var n = 1;
                for (var a = counynum - 1; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 3; i++)
                {
                    m = m * i;
                }
                result = n / m;
            }
            if (count == 3 && counynum > 0)
            {
                result = counynum;
            }
            if ((count == 0) || ((count + counynum) < 4))
            {
                result = 0;
            }
            return result;
        }
        public int RenWuDanTuo(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            int result = 0;
            int count = str1.Length;
            int counynum = str2.Length;
            if (str1.Count() > 4 || str2.Count() + str1.Count() < 6 || str1.Count()==0)
            {
                throw new ValidateException("选择的号码格式不正确！");
            }
            if (count == 1 && counynum > 3)
            {
                var n = 1;
                for (var a = counynum - 3; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 5; i++)
                {
                    m = m * i;
                }
                result = n / m;
            }
            if (count == 2 && counynum > 2)
            {
                var n = 1;
                for (var a = counynum - 2; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 4; i++)
                {
                    m = m * i;
                }
                result = n / m;
            }
            if (count == 3 && counynum > 1)
            {
                var n = 1;
                for (var a = counynum - 1; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 3; i++)
                {
                    m = m * i;
                }
                result = n / m;
            }
            if (count == 4 && counynum > 0)
            {
                result = counynum;
            }
            if (count == 0 || (count + counynum) < 5)
            {
                result = 0;
            }
            return result;
        }
        public int QianYiShuTou(string userChoseNumber) 
        {
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            var num1 = list[0].ToString();
            int result=0;
            String[] str1 = num1.Split(' ');
            if (list.Count() > 1)
            {
                var num2 = list[1].ToString();
                string[] str2 = num2.Split(' ');
                 result = str2.Length + str1.Length;
            }
            else 
            {
                result = str1.Length;
            }
           
            
            return result;
        }
        public int XuanErLianZhi(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            int result1= str1.Length;
            int result2=str2.Length;
            int count = 0;
             if (result1 == 0 || result2 == 0) {
                 count = 0;
                        } else {

                            for (var i = 0; i < result1; i++)
                            {
                                for (var j = 0; j < result2; j++)
                                {
                                    if (num1.Split(' ')[i].ToString() != num2.Split(' ')[j].ToString())
                                        count += 1;
                                }
                            }

                        }
             return count;
        }
        public int XuanSanQianZhi(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if(list.Count()!=3)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            var num3 = list[2].ToString();
            string[] str3 = num3.Split(' ');

            int result1 = str1.Length;
            int result2 = str2.Length;
            int result3=str3.Length;
            int count = 0;
             if (result1 == 0 || result2 == 0 || result3 == 0) {
                 count = 0;
                        } else {

                            for (var i = 0; i < num1.Split(' ').Length; i++)
                            {
                                for (var j = 0; j < num2.Split(' ').Length; j++)
                                {
                                    for (var k = 0; k < num3.Split(' ').Length; k++)
                                    {
                                        if (num1.Split(' ')[i].ToString() != num2.Split(' ')[j].ToString() && num2.Split(' ')[j].ToString() != num3.Split(' ')[k].ToString() && num1.Split(' ')[i].ToString() != num3.Split(' ')[k].ToString())
                                            count += 1;
                                    }
                                }
                            }

                        }
            return count;
        }

        public int XuanErLianZuDanTuo(string userChoseNumber) 
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
             string[] str2 = num2.Split(' ');
             int cout = 0;
             if (str1.Count() > 1 || str2.Count() + str1.Count() < 3 || str1.Count() == 0)
             {
                 throw new ValidateException("选择的号码不正确！");
             }
             if (str1.Length == 0)
             {
                 cout = 0;
             }
             else 
             {
                 cout = str2.Length;
             }
             return cout;
        }

        public int XuanSanQianZuDanTuo(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            String[] str1 = num1.Split(' ');
            var num2 = list[1].ToString();
            string[] str2 = num2.Split(' ');
            var result = 0;
            int count = str1.Length;
            int counynum = str2.Length;
            if (str1.Count() > 2 || str2.Count() + str1.Count() < 4 || str1.Count() == 0)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            if (count == 1 && counynum > 1)
            {
                var n = 1;
                for (var a = counynum - 1; a <= counynum; a++)
                {
                    n = n * a;
                }
                var m = 1;
                for (var i = 1; i < 3; i++)
                {
                    m = m * i;
                }
                result = n / m;

            }
            if (count == 2 && counynum > 0)
            {
                result = counynum;
            }
            if (count == 0 || (counynum + count) < 3)
            {
                result = 0;
            }
            return result;
        }
        public int TCQianSanZhiXuan(string userChoseNumber) 
        {
            var list = userChoseNumber.Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count()!=3)
            {
                  throw new ValidateException("选择的号码不正确！");
            }
            var num1 = list[0].ToString();
            var num2 = list[1].ToString();
            var num3 = list[2].ToString();
           
            int count = 0;
            for (var i = 0; i < list[0].Split(' ').Length; i++)
            {
                for (var j = 0; j < list[1].Split(' ').Length; j++)
                {
                    for (var k = 0; k < list[2].Split(' ').Length; k++)
                    {
                        if (list[0].Split(' ')[i].ToString() != list[1].Split(' ')[j].ToString() && list[0].Split(' ')[i].ToString() != list[2].Split(' ')[k].ToString() && list[1].Split(' ')[j].ToString() != list[2].Split(' ')[k].ToString())
                            count += 1;
                    }
                }
            }
            return count;
        }
        public int QianErZhiXuan(string userChoseNumber)
        {
            var list = userChoseNumber.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (list.Count() != 2)
            {
                throw new ValidateException("选择的号码不正确！");
            }
            int count = 0;
            for (var i = 0; i < list[0].Split(' ').Length; i++)
            {
                for (var j = 0; j < list[1].Split(' ').Length; j++)
                {
                    if (list[0].Split(' ')[i].ToString() != list[1].Split(' ')[j].ToString())
                        count += 1;
                }
            }

            return count;
        }
        /// <summary>
        /// 设置ChoseNumberList
        /// </summary>
        /// <param name="userChoseNumber"></param>
        public List<string> GetChoseNumberList(string userChoseNumber)
        {
            List<string> result = new List<string>();
            var list = userChoseNumber.Split(new string[] { "; ", ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in list)
                result.AddRange(_GetChoseNumberList(item));
            ChoseNumberList = result;
            return result;
        }
        /// <summary>
        /// 获取中奖注的号码列表
        /// </summary>
        /// <param name="userChoseNumber">用户选择的号码 格式：表示多星时用‘，’隔开，如：三星复式，234，567，23。表示单星的复式时无需隔开，如：三星复式，235679</param>
        /// <param name="openCode">当前彩种的开奖号码</param>
        /// <returns></returns>
        protected abstract IEnumerable<string> _GetWinNums(string userChoseNumber,string openCode);

        /// <summary>
        /// 验证用户购买的号码的格式是否正确 
        /// </summary>
        /// <param name="userChoseNumber">需要验证的字符串</param>
        /// <returns>如果匹配成功则返回原字符串，否则返回null</returns>
        protected virtual string _RegistNum(string userChoseNumber)
        {
            if (string.IsNullOrEmpty(userChoseNumber))
                return null;
            var number = userChoseNumber;
            if (lotteryType == LotteryType.TC11X5)
                number = number.Replace("10", "a").Replace("11", "b").Replace("0", "").Replace(" ", "");
            if (lotteryType == 0)
                return userChoseNumber;
            Regex r =  _UserChoseDoubleNumberFormat;

            if (r.IsMatch(number))
                return userChoseNumber;
            else
                return null;
        }
        /// <summary>
        /// 获取号码的注数(号码未经验证)
        /// </summary>
        /// <param name="userChoseNumber"></param>
        /// <returns></returns>
        protected abstract int _GetNumCount(string userChoseNumber);

        /// <summary>
        /// 设置ChoseNumberList
        /// </summary>
        /// <param name="userChoseNumber"></param>
        protected abstract List<string> _GetChoseNumberList(string userChoseNumber);
    }
}
